package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.UmsBestVolunteerDetailMapper;
import com.macro.mall.mapper.UmsBestVolunteerMapper;
import com.macro.mall.model.UmsBestVolunteer;
import com.macro.mall.model.UmsBestVolunteerDetail;
import com.macro.mall.model.UmsBestVolunteerDetailExample;
import com.macro.mall.model.UmsBestVolunteerExample;
import com.macro.mall.service.UmsBestVolunteerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UmsBestVolunteerServiceImpl implements UmsBestVolunteerService {
    @Autowired
    private UmsBestVolunteerMapper bestVolunteerMapper;

    @Autowired
    private UmsBestVolunteerDetailMapper bestVolunteerDetailMapper;

    @Override
    public List<UmsBestVolunteer> list(UmsBestVolunteer bestVolunteer,Integer pageSize,Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        UmsBestVolunteerExample example = new UmsBestVolunteerExample();
        UmsBestVolunteerExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isBlank(bestVolunteer.getStatus())){
            criteria.andStatusEqualTo(bestVolunteer.getStatus());
        }
        if(!StringUtils.isBlank(bestVolunteer.getTitle())){
            criteria.andTitleLike("%"+bestVolunteer.getTitle().trim()+"%");
        }
        if(bestVolunteer.getPosition()!=null){
            criteria.andPositionEqualTo(bestVolunteer.getPosition());
        }
        example.setOrderByClause(" position asc,mod_date desc ");
        List<UmsBestVolunteer> list=  bestVolunteerMapper.selectByExample(example);

        return list;
    }
    @Override
    public UmsBestVolunteer getById(Long id) throws Exception{
        UmsBestVolunteer bestVolunteer = bestVolunteerMapper.selectByPrimaryKey(id);
        if(bestVolunteer!=null){
            UmsBestVolunteerDetailExample example = new UmsBestVolunteerDetailExample();
            UmsBestVolunteerDetailExample.Criteria criteria = example.createCriteria();
            criteria.andVolunteerIdEqualTo(bestVolunteer.getId());
            List<UmsBestVolunteerDetail> list = bestVolunteerDetailMapper.selectByExampleWithBLOBs(example);
            if(list!=null&&list.size()>0){
                bestVolunteer.setBestVolunteerDetail(list.get(0));
            }
        }
        return bestVolunteer;
    }
    @Override
    public void delById(Long id) throws Exception{
        bestVolunteerMapper.deleteByPrimaryKey(id);
    }

    public void save(UmsBestVolunteer bestVolunteer) throws Exception {
        Date curDate = new Date();
        if (bestVolunteer.getId() != null && bestVolunteer.getId() > 0) {
            UmsBestVolunteer bestVolunteerUpdate = new UmsBestVolunteer();
            bestVolunteerUpdate.setStatus("1");
            bestVolunteerUpdate.setModDate(curDate);
            bestVolunteerUpdate.setId(bestVolunteer.getId());
            bestVolunteerMapper.updateByPrimaryKeySelective(bestVolunteerUpdate);
        }
        bestVolunteer.setId(null);
        bestVolunteer.setModDate(curDate);
        bestVolunteer.setStatus("0");
        bestVolunteerMapper.insert(bestVolunteer);
        if(bestVolunteer.getBestVolunteerDetail()!=null){
            bestVolunteer.getBestVolunteerDetail().setVolunteerId(bestVolunteer.getId());
            bestVolunteerDetailMapper.insert(bestVolunteer.getBestVolunteerDetail());
        }
    }
}